const Vue = require('vue');

const createApp = require('./app')

const server = require('express')();

const template = require('fs').readFileSync('./index.template.html', 'utf-8');

const renderer = require('vue-server-renderer').createRenderer({
    template,
});

const context = {
    title: 'vue ssr',
    url: '',
    meta: `
        <meta charset="UTF-8">
        <meta name="keyword" content="vue,ssr">
        <meta name="description" content="vue srr demo">
    `,
};

server.get('/', (req, res) => {
    context.url = req.path
    const app = createApp(context)
    renderer.renderToString(app, context, (err, html) => {
        if (err) {
            console.log(err);
            res.status(500).end('Internal Server Error')
            return;
        }
        // console.log(html);
        res.end(html);
    });
})

server.listen(8080, '127.0.0.1');
